<template>
  <div class="pagination_box">
    <div class="center">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="currentPage" :page-size.sync="pageSize" :page-sizes="pageSizes" :layout="layout" :total="total" :pager-count="pagerCount">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "Pagination",
  components: {},
  props: {
    total: {
      required: true,
      type: Number
    },
    page: {
      type: Number,
      default: 1
    },
    limit: {
      type: Number,
      default: 10
    },
    pageSizes: {
      type: Array,
      default () {
        return [10, 20, 30, 50]
      }
    },
    layout:{
      type: String,
      default: 'total, sizes, prev, pager, next, jumper'
    },
    pagerCount: {
      type: Number,
      default: 7
    }
  },
  data () {
    return {

    }
  },
  computed: {
    currentPage: {
      get () {
        return this.page
      },
      set (val) {
        this.$emit('update:page', val)
      }
    },
    pageSize: {
      get () {
        return this.limit
      },
      set (val) {
        this.$emit('update:limit', val)
      }
    }
  },
  methods: {
    handleSizeChange (val) {
      if (this.currentPage * val > this.total) {
        this.currentPage = 1
      }
      this.$emit('change', { page: this.currentPage, limit: val })
    },
    handleCurrentChange (val) {
      this.$emit('change', { page: val, limit: this.pageSize })
    }
  }
}
</script>

<style scoped lang="less">
.pagination_box {
  display: flex;
  justify-content: right;
  //margin-top: 25px;

  .center {
    display: flex;
    align-items: center;
    padding: 0 20px;
    //background-color: #e5e9f0;

    ::v-deep {
      .btn-prev,
      .btn-next {
        padding: 0 6px;
        background-color: transparent;
      }

      .el-pager {
        margin: 0 4px;

        li {
          background-color: transparent;

          &:nth-of-type(n + 2) {
            margin-left: 4px;
          }

          &.active {
            color: #266eff;
          }
        }
      }
    }
  }
}
</style>
